package com.tee.web.action;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.tee.bean.privilege.Department;
import com.tee.bean.privilege.Gender;
import com.tee.bean.privilege.PrivilegeGroup;
import com.tee.bean.privilege.Role;
import com.tee.bean.privilege.SystemPrivilege;
import com.tee.bean.privilege.User;
import com.tee.service.privilege.DepartmentService;
import com.tee.service.privilege.PrivilegeGroupService;
import com.tee.service.privilege.SystemPrivilegeService;
import com.tee.service.privilege.UserService;
import com.tee.utils.SiteUrl;

/**
 * 系统初始化
 */
@Controller("SystemInit")
public class InitAction extends BaseAction {

	private static final long serialVersionUID = -1816075356461605461L;

	@Resource
	SystemPrivilegeService privilegeService;
	@Resource
	PrivilegeGroupService groupService;
	@Resource
	UserService userService;
	@Resource
	DepartmentService departmentService;

	public String execute() throws Exception {

		initSystemPrivilege();
		initSystemGroup();
		initUser();
		request.setAttribute("message", "初始化成功");
		request.setAttribute("urladdress", SiteUrl.readUrl("logon"));
		return "message";
	}

	// 初始化用户
	private void initUser() {
		if (userService.getCount() == 0) {
			User user = null;
			user = new User();
			user.setGender(Gender.MAN);
			user.setEmail("admin@julongye.com");
			user.setPhone("admin");
			user.setPassword("admin");
			user.setRealname("低调");
			user.setRole(Role.ADMIN);
			user.setVisible(true);
			departmentService.save(new Department("行政部"));// 初始一个部门
			user.setDepartment(departmentService.find(1));
			user.getGroups().addAll(
					groupService.getScrollData().getResultlist());
			userService.save(user);
		}
	}

	// 初始化权限组
	private void initSystemGroup() {
		PrivilegeGroup group = null;
		if (groupService.getCount() == 0) {

			group = new PrivilegeGroup();
			group.setName("网站系统管理员");
			group.getPrivileges().addAll(
					privilegeService.getScrollData().getResultlist());
			groupService.save(group);
/*
			group = new PrivilegeGroup();
			group.setName("总经理");
			Object params[] = { "库存管理", "订单管理" };
			group
					.getPrivileges()
					.addAll(
							privilegeService
									.list(
											"from SystemPrivilege o where o.name not in(?1,?2)",
											params));
			groupService.save(group);

			group = new PrivilegeGroup();
			group.setName("营销总监");
			Object params1[] = { "店铺管理", "查看店铺", "统计管理", "进货统计",
					"订单管理", "查看订单", "查看进货单" };
			group
					.getPrivileges()
					.addAll(
							privilegeService
									.list(
											"from SystemPrivilege o where o.name in(?1,?2,?3,?4,?5,?6,?7)",
											params1));			
			groupService.save(group);
*/
		}
	}

	// 初始化系统权限
	private void initSystemPrivilege() {
		if (privilegeService.getCount() == 0) {
			List<SystemPrivilege> privileges = new ArrayList<SystemPrivilege>();

			// 人员管理
			privileges.add(new SystemPrivilege("system", "userManage", "人员管理"));
			privileges.add(new SystemPrivilege("user", "insert", "添加用户"));
			privileges.add(new SystemPrivilege("user", "update", "更新用户"));
			privileges.add(new SystemPrivilege("user", "view", "查看用户")); // 即人员列表
			privileges.add(new SystemPrivilege("user", "query", "查找用户"));
			privileges.add(new SystemPrivilege("user", "enable", "启用用户"));
			privileges.add(new SystemPrivilege("user", "disable", "禁用用户"));
			privileges.add(new SystemPrivilege("user", "privilegeGroupSet",	"设置用户权限"));

			// 部门管理
			privileges.add(new SystemPrivilege("department", "insert", "添加部门"));
			privileges.add(new SystemPrivilege("department", "update", "更新部门"));
			privileges.add(new SystemPrivilege("department", "view", "查看部门"));
			privileges.add(new SystemPrivilege("department", "delete", "删除部门"));

			// 权限管理
			privileges.add(new SystemPrivilege("system", "privilegeManage","权限管理"));
			privileges.add(new SystemPrivilege("privilegeGroup", "delete","删除权限"));
			privileges.add(new SystemPrivilege("privilegeGroup", "insert","添加权限"));
			privileges.add(new SystemPrivilege("privilegeGroup", "update","更新权限"));
			privileges.add(new SystemPrivilege("privilegeGroup", "view", "查看权限"));


			privilegeService.batchSave(privileges);
		}
	}

}
